package com.quanyan.user.service.impl;

import com.google.common.base.Function;
import com.google.common.collect.Maps;
import com.quanyan.api.APIResponse;
import com.quanyan.user.mapper.TbUserFollowMapper;
import com.quanyan.user.mapper.TbUserFriendMapper;
import com.quanyan.user.model.db.TbUserFollow;
import com.quanyan.user.model.db.TbUserFollowExample;
import com.quanyan.user.model.db.TbUserFriend;
import com.quanyan.user.model.db.TbUserFriendExample;
import com.quanyan.user.service.UserFriendService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by Administrator on 2016/11/16.
 */
@Service
public class UserFriendServiceImpl implements UserFriendService {
    private static final Logger logger = LoggerFactory.getLogger(UserFriendServiceImpl.class);

    @Autowired
    private TbUserFriendMapper tbUserFriendMapper;

    @Override
    public List<TbUserFriend> batchUserFriendService(Integer uid ,List<Integer> lstFid){
        //todo 加缓存
        TbUserFriendExample tbUserFriendExample = new TbUserFriendExample();
        TbUserFriendExample.Criteria criteria = tbUserFriendExample.createCriteria();
        criteria.andStateEqualTo(friend_true);
        criteria.andFriendIdIn(lstFid);
        criteria.andUidEqualTo(uid);
        return tbUserFriendMapper.selectByExample(tbUserFriendExample);
    }

    @Override
    public Map<Integer,TbUserFriend> batchUserFriendServiceToMap (Integer uid ,List<Integer> lstFid){
        List<TbUserFriend> lst =  this.batchUserFriendService(uid,lstFid);
        Map<Integer,TbUserFriend> map = new HashMap<>();
        if (lst!=null && lst.size() > 0)
           map = Maps.uniqueIndex(lst, this.indexOriginDtoByVendorItemId());
        return map;
    }

    public Function<TbUserFriend, Integer> indexOriginDtoByVendorItemId() {
        return new Function<TbUserFriend, Integer>() {
            @Override
            public Integer apply(TbUserFriend input) {
                return input.getFriendId();
            }
        };
    }

}
